package com.nowcoder.community.controller;

import com.nowcoder.community.util.CommunityUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@RestController
@RequestMapping("/alpha")
public class AlphaController {
    @RequestMapping("/Hello")
//    @ResponseBody
    public String sayHello(){
        return "/student";
    }
    @RequestMapping("http")
    public void http(HttpServletRequest request, HttpServletResponse response){
        System.out.println(request.getMethod());
        System.out.println(request.getContextPath());
        Enumeration<String> enumeration = request.getHeaderNames();
        while(enumeration.hasMoreElements()){
            String name = enumeration.nextElement();
            String value = request.getHeader(name);
            System.out.println(value);
        }
        //设置响应体的格式
        response.setContentType("text/html;charset=utf-8");
        try (PrintWriter writer = response.getWriter()){
            writer.write("<h1>牛客网</h1>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/hello")
    public List<Map<String,Object>> hello(){
        List<Map<String, Object>> list = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("1","asdasdsad");
        map.put("2","asdasdsad");
        map.put("3","asdasdsad");
        map.put("4","asdasdsad");
        map.put("5","asdasdsad");
        map.put("122","asdasdsad");
        map.put("6","asdasdsad");
        map.put("7","asdasdsad");
        list.add(map);
        list.add(map);
        return list;
    }
    //cookie示例
    @RequestMapping(path = "/cookie/set",method = RequestMethod.GET)
    @ResponseBody
    public String setCookie(HttpServletResponse response){
        //创建cookie
        Cookie cookie = new Cookie("code", "123cookievalue");
        //设置cookie的生效范围
        cookie.setPath("/alpha");
        //设置cookie的生存时间,单位秒
        cookie.setMaxAge(60*10);
        //发送cookie
        response.addCookie(cookie);
        return "set Cookie";
    }

}
